package com.example.ioc.context;

import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
 * {@link ConfigurableApplicationContext#addProtocolResolver(ProtocolResolver)}
 * {@link org.springframework.context.ApplicationContextInitializer#initialize(ConfigurableApplicationContext)}
 */
@Slf4j
public class LProtocolResolver implements ProtocolResolver {

    private static volatile LProtocolResolver instance;

    private static boolean isLogged = false;

    private LProtocolResolver() {
        Statistics.logInfo(log);
    }

    public static LProtocolResolver getInstance() {
        if (instance == null) {
            synchronized (LProtocolResolver.class) {
                if (instance == null) {
                    instance = new LProtocolResolver();
                }
            }
        }
        return instance;
    }

    @Override
    public Resource resolve(@NonNull String location, @NonNull ResourceLoader resourceLoader) {
        if (!isLogged){
            Statistics.logInfo(log, "resolve", location);
            isLogged = true;
        }
        return null;
    }
}
